package com.servlet;

import com.dao.imp.FindUser;
import com.entity.User;
import net.sf.json.JSONObject;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * @author Mathison
 * @Title: ${NAME}
 * @Package: ${PACKAGE_NAME}
 * @Description: TODO
 * @date 2021/11/29 19:17
 **/

@WebServlet(name = "PhoneAndEmailCheckServlet", value = "/PhoneAndEmailCheckServlet")
public class PhoneAndEmailCheckServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String phoneNum = request.getParameter("phone");
        String eMail = request.getParameter("email");

        String emailSql = "SELECT * FROM users WHERE user_email = '"+eMail+"'";
        String phoneSql = "SELECT * FROM users WHERE user_phone = '"+phoneNum+"'";

//        System.out.println(emailSql+","+phoneSql);

        User email = FindUser.findUser(emailSql);
        User phone = FindUser.findUser(phoneSql);

//        System.out.println(email.toString());
//        System.out.println(phone.toString());

        String jsonString = "";
        if (email.getUser_id()==0&&phone.getUser_id()==0){
            jsonString = "{status:0}";
        }else {
            if (email.getUser_id()!=0&&phone.getUser_id()!=0){
                jsonString = "{status:1,email:1,phone:1}";
            }else {
                if (email.getUser_id()!=0){
                    jsonString = "{status:1,email:1,phone:0}";
                }else {
                    jsonString = "{status:1,email:0,phone:1}";
                }
            }
        }

//        System.out.println(jsonString);

        JSONObject json =JSONObject.fromObject(jsonString);
        response.getWriter().print(json);

    }
}
